package org.roger.analyzer;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.core.LowerCaseFilter;
import org.apache.lucene.analysis.standard.StandardTokenizer;
import org.roger.index.filter.MetaphoneReplacementFilter;

/**
 * 同音词分析器
 * @author luoweiyi
 */
public class MetaPhoneAnalyzer extends Analyzer{

	/**
	 *先用StandardTokenizer作分词器，再通过LowerCaseFilter和MetaphoneReplacementFilter过滤
	 */
	@Override
	protected TokenStreamComponents createComponents(String fieldName) {
		
//		synchronized(this){
//			return new Analyzer.TokenStreamComponents(null, new MetaphoneReplacementFilter(new LetterTokenizer(reader)))
//		    {
//		      protected void setReader(Reader reader) throws IOException {
//		        super.setReader(reader);
//		      }
//		    };
//		}
		final StandardTokenizer src = new StandardTokenizer();
		TokenStream tok = new LowerCaseFilter(src);
	    tok = new MetaphoneReplacementFilter(tok);
	    
	    return new TokenStreamComponents(src, tok);
		
		//return new TokenStreamComponents(new StandardTokenizer(reader), new MetaphoneReplacementFilter(new StandardTokenizer(reader)));
	}
	
}
